package com.lanou.mynews.movie;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.mynews.R;

import java.util.List;


/**
 * Created by dllo on 16/1/18.
 */
public class MovieFragment extends Fragment implements AdapterView.OnItemClickListener {

    private GridView gridView;
    private HomeAdapter homeAdapter;
    private List<Home.FilmListEntity> filmList;
    private RequestQueue requestQueue;



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.activity_home,container,false);

        gridView = (GridView) v.findViewById(R.id.gridView);
        gridView.setOnItemClickListener(this);
        return v;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        requestQueue = Volley.newRequestQueue(getActivity());// 初始化请求队列
        getData();
    }
    private void getData() {
        String getUrl = "http://mobileif.maizuo.com/version3.1.1/city/52/film?" +
                "index=0&count=10&type=1&clientID=31";
        System.out.println(getUrl);
        StringRequest request = new StringRequest(getUrl,
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String response) {// 网址正确的时候会回调的方法
                        if (response != null) {// response是从网络返回的全部数据,字符串类型
                            Gson gson = new Gson();
                            // 将解析的字符串类型的值,转成TicketResult中的格式
                            Home result = gson.fromJson(response,
                                    Home.class);
                            if (result != null && result.getFilmList() != null) {
                                filmList = result.getFilmList();
                                Log.d("lanou", "onResponse() called with: " + "response = [" + response + "]");
                                //绑定适配器
                                homeAdapter = new HomeAdapter(getActivity(), filmList);
                                gridView.setAdapter(homeAdapter);

                            }
                        }
                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                // TODO Auto-generated method stub

            }
        });
        // 将图片请求对象加入到请求队列中
        requestQueue.add(request);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(getActivity(), ContentActivity.class);
        intent.putExtra("id",filmList.get(position).getFilmID());
        startActivity(intent);
    }
}






